package com.zhu.salesmanagement;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.dms2.salesmanagement.R;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.common.Log;
import com.zhu.salesmanagement.models.AddedStoreItemInfo;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.provider.SalesSettings.MyAddStoresColumns;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * 门店属性调查界面
 */

public class StoreInfoActivity extends Activity implements View.OnClickListener{

    public static final int TYPE_FROMVISIT = 1;
    public static final String STR_TYPE = "type";
    public static final String STR_STOREID = "cid";
    public static final String TYPE_CUSTOMER = "customer";
    public static final String TYPE_NEWSTORE= "newstore";
    private EditText mSp1View = null;
    private EditText mSp2View = null;
    private EditText mSp3View = null;
    private EditText mSp4View = null;
    private EditText mSp5View = null;
    private EditText mSp6View = null;
    private CheckBox mSp7View = null;
    private CheckBox mSp8View = null;
    private CheckBox mSp9View = null;
    private CheckBox mSp10View = null;
    private CheckBox mSp11View = null;
    private CheckBox mSp12View = null;
    private CheckBox mSp13View = null;
    private EditText mSp14View = null;

    private AddedStoreItemInfo mAddedStoreItemInfo = null;
    private int mCurtype = 0;
    private String mStoreid = "";
    private long mDbid = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_storeinfo);

        mCurtype = getIntent().getIntExtra(STR_TYPE, 0);
        mStoreid = getIntent().getStringExtra(STR_STOREID);
        Log.e("mStoreid="+mStoreid);

        Parcelable parcelable = getIntent().getParcelableExtra("info");
        mAddedStoreItemInfo = (AddedStoreItemInfo) parcelable;

        setupView();
        initData();
    }

    public void onResume() {
        super.onResume();
        Actions.setAutoTime(this);
    }

    /**
     * 初始化界面
     */
    private void setupView(){
        findViewById(R.id.storeinfo_back).setOnClickListener(this);
        findViewById(R.id.storeinfo_bottom_back).setOnClickListener(this);

        mSp1View = (EditText)findViewById(R.id.storeinfo_system);
        mSp2View = (EditText)findViewById(R.id.storeinfo_area);
        mSp3View = (EditText)findViewById(R.id.storeinfo_pnumber);
        mSp4View = (EditText)findViewById(R.id.storeinfo_posnumber);
        mSp5View = (EditText)findViewById(R.id.storeinfo_allsales);
        mSp6View = (EditText)findViewById(R.id.storeinfo_oilsales);

        mSp7View = (CheckBox)findViewById(R.id.storeinfo_checkbox1);
        mSp8View = (CheckBox)findViewById(R.id.storeinfo_checkbox2);
        mSp9View = (CheckBox)findViewById(R.id.storeinfo_checkbox3);
        mSp10View = (CheckBox)findViewById(R.id.storeinfo_checkbox4);
        mSp11View = (CheckBox)findViewById(R.id.storeinfo_checkbox5);
        mSp12View = (CheckBox)findViewById(R.id.storeinfo_checkbox6);
        mSp13View = (CheckBox)findViewById(R.id.storeinfo_checkbox7);

        mSp14View = (EditText)findViewById(R.id.storeinfo_other);
    }

    /**
     * 初始化数据
     */
    private void initData(){
        if(mCurtype == TYPE_FROMVISIT){
            if(mStoreid != null && mStoreid.trim().length() > 0){
                updateFormDataBase();
            }
        }else{
            if(mAddedStoreItemInfo != null){
                mSp1View.setText(mAddedStoreItemInfo.mSp1);
                mSp2View.setText(mAddedStoreItemInfo.mSp2);
                mSp3View.setText(mAddedStoreItemInfo.mSp3);
                mSp4View.setText(mAddedStoreItemInfo.mSp4);
                mSp5View.setText(mAddedStoreItemInfo.mSp5);
                mSp6View.setText(mAddedStoreItemInfo.mSp6);

                mSp7View.setChecked(mAddedStoreItemInfo.mSp7 == 1 ? true : false);
                mSp8View.setChecked(mAddedStoreItemInfo.mSp8 == 1 ? true : false);
                mSp9View.setChecked(mAddedStoreItemInfo.mSp9 == 1 ? true : false);
                mSp10View.setChecked(mAddedStoreItemInfo.mSp10 == 1 ? true : false);
                mSp11View.setChecked(mAddedStoreItemInfo.mSp11 == 1 ? true : false);
                mSp12View.setChecked(mAddedStoreItemInfo.mSp12 == 1 ? true : false);
                mSp13View.setChecked(mAddedStoreItemInfo.mSp13 == 1 ? true : false);

                mSp14View.setText(mAddedStoreItemInfo.mSp14);
            }
        }
    }

    /**
     * 读取数据库历史数据
     */
    private void updateFormDataBase(){
        String where = SalesSettings.MyAddStoresColumns.STOREID +" = \'" + mStoreid +"\'";
        Cursor cursor = this.getContentResolver().query(SalesSettings.MyAddStoresColumns.CONTENT_URI, null, where, null, null);
        if(cursor != null && cursor.getCount() > 0){
            Log.e("mStoreid22222222="+mStoreid);
            try {
                final int idIndex = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns._ID);
                final int sp1Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP1);
                final int sp2Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP2);
                final int sp3Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP3);
                final int sp4Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP4);
                final int sp5Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP5);
                final int sp6Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP6);
                final int sp7Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP7);
                final int sp8Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP8);
                final int sp9Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP9);
                final int sp10Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP10);
                final int sp11Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP11);
                final int sp12Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP12);
                final int sp13Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP13);
                final int sp14Index = cursor.getColumnIndexOrThrow(SalesSettings.MyAddStoresColumns.SP14);

                while (cursor.moveToNext()) {
                    AddedStoreItemInfo info = new AddedStoreItemInfo();
                    mDbid = cursor.getLong(idIndex);
                    info.mSp1 = cursor.getString(sp1Index);
                    info.mSp2 = cursor.getString(sp2Index);
                    info.mSp3 = cursor.getString(sp3Index);
                    info.mSp4 = cursor.getString(sp4Index);
                    info.mSp5 = cursor.getString(sp5Index);
                    info.mSp6 = cursor.getString(sp6Index);
                    info.mSp7 = cursor.getInt(sp7Index);
                    info.mSp8 = cursor.getInt(sp8Index);
                    info.mSp9 = cursor.getInt(sp9Index);
                    info.mSp10 = cursor.getInt(sp10Index);
                    info.mSp11 = cursor.getInt(sp11Index);
                    info.mSp12 = cursor.getInt(sp12Index);
                    info.mSp13 = cursor.getInt(sp13Index);
                    info.mSp14 = cursor.getString(sp14Index);

                    mSp1View.setText(info.mSp1);
                    mSp2View.setText(info.mSp2);
                    mSp3View.setText(info.mSp3);
                    mSp4View.setText(info.mSp4);
                    mSp5View.setText(info.mSp5);
                    mSp6View.setText(info.mSp6);

                    mSp7View.setChecked(info.mSp7 == 1 ? true : false);
                    mSp8View.setChecked(info.mSp8 == 1 ? true : false);
                    mSp9View.setChecked(info.mSp9 == 1 ? true : false);
                    mSp10View.setChecked(info.mSp10 == 1 ? true : false);
                    mSp11View.setChecked(info.mSp11 == 1 ? true : false);
                    mSp12View.setChecked(info.mSp12 == 1 ? true : false);
                    mSp13View.setChecked(info.mSp13 == 1 ? true : false);

                    mSp14View.setText(info.mSp14);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        }
        if(cursor != null){
            cursor.close();
            cursor = null;
        }
    }

    /**
     * 点击事件处理
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v != null){
            switch(v.getId()){
                case R.id.storeinfo_bottom_back:
                    if(mCurtype == TYPE_FROMVISIT){
                        saveToDataBase();
                        finish();
                    }else{
                        saveData();
                        Intent intent = new Intent();
                        intent.putExtra("resultinfo", mAddedStoreItemInfo);
                        setResult(RESULT_OK, intent);
                    }
                case R.id.storeinfo_back:
                    finish();
                    break;
            }
        }
    }

    /**
     * 保存数据
     */
    private void saveData(){
        if(mAddedStoreItemInfo == null){
            mAddedStoreItemInfo = new AddedStoreItemInfo();
        }
        mAddedStoreItemInfo.mSp1 = mSp1View.getText().toString();
        mAddedStoreItemInfo.mSp2 = mSp2View.getText().toString();
        mAddedStoreItemInfo.mSp3 = mSp3View.getText().toString();
        mAddedStoreItemInfo.mSp4 = mSp4View.getText().toString();
        mAddedStoreItemInfo.mSp5 = mSp5View.getText().toString();
        mAddedStoreItemInfo.mSp6 = mSp6View.getText().toString();

        mAddedStoreItemInfo.mSp7 = mSp7View.isChecked() ? 1 : 0;
        mAddedStoreItemInfo.mSp8 = mSp8View.isChecked() ? 1 : 0;
        mAddedStoreItemInfo.mSp9 = mSp9View.isChecked() ? 1 : 0;
        mAddedStoreItemInfo.mSp10 = mSp10View.isChecked() ? 1 : 0;
        mAddedStoreItemInfo.mSp11 = mSp11View.isChecked() ? 1 : 0;
        mAddedStoreItemInfo.mSp12 = mSp12View.isChecked() ? 1 : 0;
        mAddedStoreItemInfo.mSp13 = mSp13View.isChecked() ? 1 : 0;

        mAddedStoreItemInfo.mSp14 = mSp14View.getText().toString();
    }

    /**
     * 保存数据至数据库
     */
    private void saveToDataBase(){
        saveData();
        ContentValues values = new ContentValues();
        ContentResolver cr = getContentResolver();
        if(mAddedStoreItemInfo != null){
            values.put(SalesSettings.MyAddStoresColumns.SP1, mAddedStoreItemInfo.mSp1);
            values.put(SalesSettings.MyAddStoresColumns.SP2, mAddedStoreItemInfo.mSp2);
            values.put(SalesSettings.MyAddStoresColumns.SP3, mAddedStoreItemInfo.mSp3);
            values.put(SalesSettings.MyAddStoresColumns.SP4, mAddedStoreItemInfo.mSp4);
            values.put(SalesSettings.MyAddStoresColumns.SP5, mAddedStoreItemInfo.mSp5);
            values.put(SalesSettings.MyAddStoresColumns.SP6, mAddedStoreItemInfo.mSp6);
            values.put(SalesSettings.MyAddStoresColumns.SP7, mAddedStoreItemInfo.mSp7);
            values.put(SalesSettings.MyAddStoresColumns.SP8, mAddedStoreItemInfo.mSp8);
            values.put(SalesSettings.MyAddStoresColumns.SP9, mAddedStoreItemInfo.mSp9);
            values.put(SalesSettings.MyAddStoresColumns.SP10, mAddedStoreItemInfo.mSp10);
            values.put(SalesSettings.MyAddStoresColumns.SP11, mAddedStoreItemInfo.mSp11);
            values.put(SalesSettings.MyAddStoresColumns.SP12, mAddedStoreItemInfo.mSp12);
            values.put(SalesSettings.MyAddStoresColumns.SP13, mAddedStoreItemInfo.mSp13);
            values.put(SalesSettings.MyAddStoresColumns.SP14, mAddedStoreItemInfo.mSp14);
        }
        if (mDbid == -1) {
            values.put(SalesSettings.MyAddStoresColumns.NAME, getTime());
            values.put(SalesSettings.MyAddStoresColumns.STOREID, mStoreid);
            values.put(SalesSettings.MyAddStoresColumns.TYPE, TYPE_CUSTOMER);
            cr.insert(SalesSettings.MyAddStoresColumns.CONTENT_URI, values);
        } else {
            values.put(SalesSettings.MyAddStoresColumns._ID, mDbid);
            cr.update(SalesSettings.MyAddStoresColumns.getContentUri(mDbid, false), values, null,
                    null);
        }
    }

    /**
     * 获取时间
     */
    private String getTime(){
        Date now = new Date();
        SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd");
        String str=temp.format(now);
        Log.e(str);
        return str;
    }
}
